﻿SmitearWebGL.Scene.QuadTreeRect = Class.extend(
{
	/* * * * * * * * constructor * * * * * * * */

	init : function( x0, x1, y0, y1, z0, z1 )
	{
		this._super();
		this._registClass( "SmitearWebGL.Scene.QuadTreeRect" );

		/* * * * * * * * public property * * * * * * * */

		this.x0 = x0 || 0.0;
		this.x1 = x1 || 0.0;
		this.y0 = y0 || 0.0;
		this.y1 = y1 || 0.0;
		this.z0 = z0 || 0.0;
		this.z1 = z1 || 0.0;

		/* * * * * * * * private property * * * * * * * */
	}
	,
	copy : function( qRect )
	{
		this.x0 = qRect.x0;
		this.x1 = qRect.x1;
		this.y0 = qRect.y0;
		this.y1 = qRect.y1;
		this.z0 = qRect.z0;
		this.z1 = qRect.z1;
	}
	,
	clone : function()
	{
		return new SmitearWebGL.Scene.QuadTreeRect( this.x0, this.x1, this.y0, this.y1, this.z0, this.z1 );
	}
	,
	convert : function( worldRect, offset, scale )
	{
		var convertedRect = worldRect.clone();

		convertedRect.addVector( offset );
		convertedRect.mulVector( scale );

		convertedRect.x1 -= 0.01;
		convertedRect.y1 -= 0.01;
		convertedRect.z1 -= 0.01;

		convertedRect.x1 = Math.max( convertedRect.x1, convertedRect.x0 );
		convertedRect.y1 = Math.max( convertedRect.y1, convertedRect.y0 );
		convertedRect.z1 = Math.max( convertedRect.z1, convertedRect.z0 );

		this.x0 = Math.floor( convertedRect.x0 );
		this.x1 = Math.floor( convertedRect.x1 );
		this.y0 = Math.floor( convertedRect.y0 );
		this.y1 = Math.floor( convertedRect.y1 );
		this.z0 = Math.floor( convertedRect.z0 );
		this.z1 = Math.floor( convertedRect.z1 );

		this.x0 = SmitearWebGL.Core.Math.clamp( this.x0, 0, 254 );
		this.y0 = SmitearWebGL.Core.Math.clamp( this.y0, 0,  30 );
		this.z0 = SmitearWebGL.Core.Math.clamp( this.z0, 0, 254 );

		this.x1 = SmitearWebGL.Core.Math.clamp( this.x1, this.x0 + 1, 255 );
		this.y1 = SmitearWebGL.Core.Math.clamp( this.y1, this.y0 + 1,  31 );
		this.z1 = SmitearWebGL.Core.Math.clamp( this.z1, this.z0 + 1, 255 );
	}
}
);